Discord python bot logging

Viewed 97

So hey there... My bot has a Logging part, which just prints message and message author in the console...

import discord

class MyClient(discord.Client):
    async def on_ready(self):
        print('bot is running')

    async def on_message(self, message):
        if message.author == client.user:
            return

        print(str(message.author) + ": " + str(message.content))

client = MyClient()
client.run("token")

But I want to know, if I can print, on which server the message got written... I know, that you can import "guild" but I dont know, if it can be used for this. If yes, then how? Thanks for helping :D

1 Answers

You need not import anything more

you can slightly modify the print command to

print(f"message : {message.content}\nchannel: {message.channel}\n author: {message.author}\n server: {message.guild.name}")

this is using fstring if you want this like you have written, the code will be like

print(str(message.author) + ": " + str(message.content) + "(" + message.guild.name + ")" )
Related