I’m developing a Discord bot and facing challenges with implementing an embed for my command that checks network connectivity. Initially, I had a simple setup that just returned text, but I wanted to enhance the appearance using Discord embeds.
Initial version without embed:
import subprocess
@client.command()
async def check_host(ctx, *, target_ip: str = '8.8.8.8'):
host_list = [target_ip]
for host in host_list:
result = subprocess.run(['ping', '-c', '4', host], capture_output=True, text=True)
if result.returncode == 0:
await ctx.send(f"ONLINE {host} - Connection successful")
else:
await ctx.send(f"OFFLINE {host} - Connection failed")
Updated version with embed:
@client.command()
async def check_host(ctx, *, target_ip: str = '8.8.8.8'):
host_list = [target_ip]
status_embed = discord.Embed(title='Network Status:', color=0x00FF00)
status_embed.set_footer(text=f"Command used by {ctx.author}")
for host in host_list:
result = subprocess.run(['ping', '-c', '4', host], capture_output=True, text=True)
if result.returncode == 0:
status_embed.add_field(name=f"{host} is ONLINE", value="Connection established", inline=False)
else:
status_embed.add_field(name=f"{host} is OFFLINE", value="Connection timeout", inline=False)
await ctx.send(embed=status_embed)
The key was to create a discord.Embed object and utilize add_field() for a structured display of the results.