async def on_message(self, message):
`if message.author == self.user:`
`return`
if message.content.lower() == 'hi':
`await message.channel.send('hello')`
if message.content.lower() == '':
`f = open("Path/To/Your/File.txt", "w") # 'r' for reading and 'w' for writing`
`f.write("" f.name) # Write inside file`
`f.close()`
If someone sends a message in a discord channel, and the message does not contain the word, "hi", I want it to save it the unknown message into an existing text file. Please help me, thanks :) Without using client commands or code.
CodePudding user response:
I know you asked not to receive code, but I am going to have to. This portion:
if message.content.lower() == '':
f = open("Path/To/Your/File.txt", "w")
f.write("" f.name)
f.close()
Seems to check if a user sends a blank message (should be impossible, since you have to send some thing in discord), and opens Path/To/Your/File.txt in write mode, and writes in the name of it (File.txt). Also, lower() makes the text lowercase, and that is not needed since you cant have an uppercase nothing.
Also, the Path/To whatever means you would have to create three folders, path, to, and your, and then put a File.txt in your. You should put a file.txt in your current folder, and change that /Path/To to just file.txt.
It does not check if the content is not hi.
You should completely remove that section.
Step 1:
Create a file called like log.txt.
Step two:
Replace that section of code with:
if message.content != 'hi':
f = open("log.txt", "a") #or what you called it. also a for append, it would overwrite otherwise.
f.write(message.content "\n") #\n means new line
f.close()
You seem to be a beginner, so don't worry. I remember when I was a beginner.
CodePudding user response:
According to your comment, I think you want to log the messages to a text file only if the bot does not have a response for it.
So here's how you could structure your on_message
function:
Define the function as async def on_message(self, message)
After this you can check if you sent the message:
if message.author == self.user:
return
Then you can add specific responses for specific strings:
if message.content.lower() == "hi":
await message.channel.send("hello")
return
Now's where the important part comes in:
First you create a context by using self.get_context(message)
After this you have to check if the context can be used to invoke a command. If yes, then the context is valid, and you can invoke the command. If the context is invalid, then you can log the contents to a file.
if context.valid():
await self.invoke(context)
return
If none of these if
clauses trigger, then that means the bot does not have a reponse for the message. So you can log it to the file.
with open("path/to/file.txt", 'a') as f: # a for append
f.write(message.content '\n')
So your final on_message
method will be:
async def on_message(self, message):
# Check if the bot sent the message.
if message.author == self.user:
return
# Reply to some predetermined strings of text
if message.content.lower() == "hi":
await message.channel.send("hello")
return
# Create a context from the message
context = await self.create_context(message)
# If the context is valid, then invoke the command
if context.valid:
await self.invoke(context)
return
# Append to file if the bot does not have a respone to the message
with open("path/to/file.txt", 'a') as f:
f.write(message.content '\n')