Discord Bot cannot access text files
Long story short, I have been working in a bot for the past month but I had all of my commands on on_message listeners instead of context and command decorators, so I decided to do it properly, in the process of transferring my commands numerous changes have been needed, but this one has me puzzled.
The way I store user information is in text files, unfortunately on the new way of doing things it seems that I cannot access text files anymore, I looked at this code over and over and tried to troubleshoot it by making sure strings aren't empty and such, the more I looked into it the more I realized that there isn't anything wrong with it, based on other information i've looked up online, the only reason I could think of is because code is inside an async function which causes it to not work. It does not throw any exceptions for anyone wondering
If this is indeed the case, what would I have to do to fix it?
Current code:
userid = 'userfiles/' + ctx.message.server.id + '/' + ctx.message.author.id + '.txt' #get path based on user ID
try:
userfile = open(userid, 'a') #open the file from
userfile.write(i)
userfile.close()
except IOError:
await client.send_message(ctx.message.channel, 'Irrecovarable exception encountered')
return
python-3.x discord.py
add a comment |
Long story short, I have been working in a bot for the past month but I had all of my commands on on_message listeners instead of context and command decorators, so I decided to do it properly, in the process of transferring my commands numerous changes have been needed, but this one has me puzzled.
The way I store user information is in text files, unfortunately on the new way of doing things it seems that I cannot access text files anymore, I looked at this code over and over and tried to troubleshoot it by making sure strings aren't empty and such, the more I looked into it the more I realized that there isn't anything wrong with it, based on other information i've looked up online, the only reason I could think of is because code is inside an async function which causes it to not work. It does not throw any exceptions for anyone wondering
If this is indeed the case, what would I have to do to fix it?
Current code:
userid = 'userfiles/' + ctx.message.server.id + '/' + ctx.message.author.id + '.txt' #get path based on user ID
try:
userfile = open(userid, 'a') #open the file from
userfile.write(i)
userfile.close()
except IOError:
await client.send_message(ctx.message.channel, 'Irrecovarable exception encountered')
return
python-3.x discord.py
Try running the code without the try/except so it gives you the full error message. Then post that error message here.
– Patrick Haugh
Dec 31 '18 at 15:01
I just did it, it does not give an exception or message, I printed the string to make sure it's not empty and it wasn't
– Myronaz
Dec 31 '18 at 16:44
What isi
? Are you trying to open a file that doesn't already exist, or in a directory that hasn't been created? Do you have any other error handling code that could be catching the error messages before they get to you?
– Patrick Haugh
Dec 31 '18 at 17:52
i
is the string to be written to the file, I already said that I've printed it to make sure that it it's fine, prior to this code is code that builds the string to be written and does not have any error handling. Here is the whole function code: pastebin Had to use pastebin as I can't fit the code into my comment
– Myronaz
Dec 31 '18 at 18:52
add a comment |
Long story short, I have been working in a bot for the past month but I had all of my commands on on_message listeners instead of context and command decorators, so I decided to do it properly, in the process of transferring my commands numerous changes have been needed, but this one has me puzzled.
The way I store user information is in text files, unfortunately on the new way of doing things it seems that I cannot access text files anymore, I looked at this code over and over and tried to troubleshoot it by making sure strings aren't empty and such, the more I looked into it the more I realized that there isn't anything wrong with it, based on other information i've looked up online, the only reason I could think of is because code is inside an async function which causes it to not work. It does not throw any exceptions for anyone wondering
If this is indeed the case, what would I have to do to fix it?
Current code:
userid = 'userfiles/' + ctx.message.server.id + '/' + ctx.message.author.id + '.txt' #get path based on user ID
try:
userfile = open(userid, 'a') #open the file from
userfile.write(i)
userfile.close()
except IOError:
await client.send_message(ctx.message.channel, 'Irrecovarable exception encountered')
return
python-3.x discord.py
Long story short, I have been working in a bot for the past month but I had all of my commands on on_message listeners instead of context and command decorators, so I decided to do it properly, in the process of transferring my commands numerous changes have been needed, but this one has me puzzled.
The way I store user information is in text files, unfortunately on the new way of doing things it seems that I cannot access text files anymore, I looked at this code over and over and tried to troubleshoot it by making sure strings aren't empty and such, the more I looked into it the more I realized that there isn't anything wrong with it, based on other information i've looked up online, the only reason I could think of is because code is inside an async function which causes it to not work. It does not throw any exceptions for anyone wondering
If this is indeed the case, what would I have to do to fix it?
Current code:
userid = 'userfiles/' + ctx.message.server.id + '/' + ctx.message.author.id + '.txt' #get path based on user ID
try:
userfile = open(userid, 'a') #open the file from
userfile.write(i)
userfile.close()
except IOError:
await client.send_message(ctx.message.channel, 'Irrecovarable exception encountered')
return
python-3.x discord.py
python-3.x discord.py
asked Dec 31 '18 at 14:56
MyronazMyronaz
396
396
Try running the code without the try/except so it gives you the full error message. Then post that error message here.
– Patrick Haugh
Dec 31 '18 at 15:01
I just did it, it does not give an exception or message, I printed the string to make sure it's not empty and it wasn't
– Myronaz
Dec 31 '18 at 16:44
What isi
? Are you trying to open a file that doesn't already exist, or in a directory that hasn't been created? Do you have any other error handling code that could be catching the error messages before they get to you?
– Patrick Haugh
Dec 31 '18 at 17:52
i
is the string to be written to the file, I already said that I've printed it to make sure that it it's fine, prior to this code is code that builds the string to be written and does not have any error handling. Here is the whole function code: pastebin Had to use pastebin as I can't fit the code into my comment
– Myronaz
Dec 31 '18 at 18:52
add a comment |
Try running the code without the try/except so it gives you the full error message. Then post that error message here.
– Patrick Haugh
Dec 31 '18 at 15:01
I just did it, it does not give an exception or message, I printed the string to make sure it's not empty and it wasn't
– Myronaz
Dec 31 '18 at 16:44
What isi
? Are you trying to open a file that doesn't already exist, or in a directory that hasn't been created? Do you have any other error handling code that could be catching the error messages before they get to you?
– Patrick Haugh
Dec 31 '18 at 17:52
i
is the string to be written to the file, I already said that I've printed it to make sure that it it's fine, prior to this code is code that builds the string to be written and does not have any error handling. Here is the whole function code: pastebin Had to use pastebin as I can't fit the code into my comment
– Myronaz
Dec 31 '18 at 18:52
Try running the code without the try/except so it gives you the full error message. Then post that error message here.
– Patrick Haugh
Dec 31 '18 at 15:01
Try running the code without the try/except so it gives you the full error message. Then post that error message here.
– Patrick Haugh
Dec 31 '18 at 15:01
I just did it, it does not give an exception or message, I printed the string to make sure it's not empty and it wasn't
– Myronaz
Dec 31 '18 at 16:44
I just did it, it does not give an exception or message, I printed the string to make sure it's not empty and it wasn't
– Myronaz
Dec 31 '18 at 16:44
What is
i
? Are you trying to open a file that doesn't already exist, or in a directory that hasn't been created? Do you have any other error handling code that could be catching the error messages before they get to you?– Patrick Haugh
Dec 31 '18 at 17:52
What is
i
? Are you trying to open a file that doesn't already exist, or in a directory that hasn't been created? Do you have any other error handling code that could be catching the error messages before they get to you?– Patrick Haugh
Dec 31 '18 at 17:52
i
is the string to be written to the file, I already said that I've printed it to make sure that it it's fine, prior to this code is code that builds the string to be written and does not have any error handling. Here is the whole function code: pastebin Had to use pastebin as I can't fit the code into my comment– Myronaz
Dec 31 '18 at 18:52
i
is the string to be written to the file, I already said that I've printed it to make sure that it it's fine, prior to this code is code that builds the string to be written and does not have any error handling. Here is the whole function code: pastebin Had to use pastebin as I can't fit the code into my comment– Myronaz
Dec 31 '18 at 18:52
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988769%2fdiscord-bot-cannot-access-text-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988769%2fdiscord-bot-cannot-access-text-files%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try running the code without the try/except so it gives you the full error message. Then post that error message here.
– Patrick Haugh
Dec 31 '18 at 15:01
I just did it, it does not give an exception or message, I printed the string to make sure it's not empty and it wasn't
– Myronaz
Dec 31 '18 at 16:44
What is
i
? Are you trying to open a file that doesn't already exist, or in a directory that hasn't been created? Do you have any other error handling code that could be catching the error messages before they get to you?– Patrick Haugh
Dec 31 '18 at 17:52
i
is the string to be written to the file, I already said that I've printed it to make sure that it it's fine, prior to this code is code that builds the string to be written and does not have any error handling. Here is the whole function code: pastebin Had to use pastebin as I can't fit the code into my comment– Myronaz
Dec 31 '18 at 18:52