I'm trying to make a bot that listens for messages in a channel and sends them to my private server. I don't fully understand coding stuff so I relied on some online sources.
I already have done a bot that gets the new messages in a channel then brings them to a private channel. I cannot find the GitHub where the code was located since I moved to another OS. Now that's out of the way, I need the bot to send the logs to my private server in case some of my staff accidentally deletes one of the logs/multiple log. Then I came across this similar question: How to send a message to a different server | Discord Bot , but I realized this was JavaScript not python. Nonetheless, I moved to that JavaScript and tried it.
Here's the Python bot that I got from a GitHub. It creates an embed of the message it listened and sends them to the channel it specified (#message-log). It also includes the the author's avatar and name
@client.event
async def on_message(message):
guild = message.guild
log_channel = discord.utils.get(guild.channels, name="message-log")
if log_channel is None:
await client.process_commands(message)
return
if not message.author.bot:
embed=discord.Embed(
color=0xffd700,
timestamp=datetime.datetime.utcnow(),
description="in {}:\n{}".format(message.channel.mention, message.content)
)
embed.set_author(name=message.author, icon_url=message.author.avatar_url)
embed.set_footer(text=message.author.id)
if len(message.attachments) > 0:
embed.set_image(url = message.attachments[0].url)
await log_channel.send(embed=embed)
await client.process_commands(message)
Then here's the JavaScript code (this is not my focus, but I decided to include it for emergency). It gets the listened server guild ID and channel. I don't know if it's really capable of sending it to my private server since there are errors.
client.guilds.get(<guild id>).channels.get(<channel id>).send(<message>)
I also didn't include IDs for privacy reasons.
I anticipated that JavaScript would run like it would normally would, but it returned an error below.
TypeError: Cannot read property 'channels' of undefined
at Object.<anonymous> (C:\Users\SomethingCube\Desktop\ListenBot\ListenBot.js:12:40)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Probably because the code I got (JavaScript) was made in 2018 and many things changed before I posted this, or I'm doing something wrong (I'm guessing that I placed the wrong prefix and suffix (I don't know what's that called) of the IDs, ("------------------")).
Again, JavaScript is not my focus here but Python. I'm looking for a code (for Python) that sends the messages to my private server.
get_guild
to get the guild you want to send the message to. – Patrick Haugh