Reputation: 21
I have been trying to make a chat logger for a discord server I run that looks at a conversation takes the messages and store them in a txt file. I am unsure of any ways to log this as my current code can only log contents that mention the bot itself. I do have the intent set to true on the discord application portal, do I need to actually do anything in the code to allow it to get the contents of any message?
`
@client.event
async def on_message(message):
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
usermessage = "{} {} {}".format(dt_string, message)
with open('data/discord/log.txt', 'a+') as y:
y.write(usermessage + '\n')
print(message.content)
client.run(TOKEN)
`
I tried making the message var into a str using
text = str(message.content)
but it gave the message content when the bot was mentioned.
Upvotes: 0
Views: 71
Reputation: 21
Let me save you the problem with a fix that works for me, however I'm using discord.py with pycord which is a wrapper.
@bot.listen()
async def on_message(message: discord.Message):
channel = bot.get_channel('')
messages = await channel.history(limit=1).flatten()
for message in messages:
if not message.attachments:
print(f"{message.guild.name}:", message.author, message.content)
else:
print(message.author, message.attachments)
It will print the message into your terminal, along with the author and the guild in which the message came from, hope this helps!
Upvotes: 0
Reputation: 21
Instead of using
client = discord.Client(intents=discord.Intents.default())
I should have set it to
client = discord.Client(intents=discord.Intents.all())
It was a very silly mistake of mine, I apologize.
Upvotes: 1